package com.jams.goodox.activity.mine.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jams.goodox.R;

/**
 * Created by Jin on 2016/7/5.
 * Description 永猎双子
 */
public class KindredView extends RelativeLayout {

	private LinearLayout layout;
	private TextView leftText, rightText;

	public KindredView(final Context context, AttributeSet attrs) {
		super(context, attrs);
		View view = View.inflate(context, R.layout.kindred_view, null);

		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.KindredView);

		try {
			String leftTxt = a.getString(R.styleable.KindredView_leftText);
			String rightTxt = a.getString(R.styleable.KindredView_rightText);

			layout = (LinearLayout) view.findViewById(R.id.layout);

			leftText = (TextView) view.findViewById(R.id.leftText);
			leftText.setText(leftTxt);
			leftText.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					setLeftFocus(context);
					mKindredClickListener.addKindredClickListener(0);
				}
			});

			rightText = (TextView) view.findViewById(R.id.rightText);
			rightText.setText(rightTxt);
			rightText.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					setRightFocus(context);
					mKindredClickListener.addKindredClickListener(1);
				}
			});
		} finally {
			a.recycle();
		}

		this.addView(view);
	}

	public void setFocus(Context context, int position) {
		if (position == 0)
			setLeftFocus(context);
		else if (position == 1)
			setRightFocus(context);
	}

	public void setLeftFocus(Context context) {
		layout.setBackgroundResource(R.mipmap.kindred_switch2);
		leftText.setTextColor(ContextCompat.getColor(context, R.color.font_color_white));
		rightText.setTextColor(ContextCompat.getColor(context, R.color.font_color_deep_gray));
	}

	public void setRightFocus(Context context) {
		layout.setBackgroundResource(R.mipmap.kindred_switch1);
		leftText.setTextColor(ContextCompat.getColor(context, R.color.font_color_deep_gray));
		rightText.setTextColor(ContextCompat.getColor(context, R.color.font_color_white));
	}

	private KindredClickListener mKindredClickListener;
	public void addKindredClickListener(KindredClickListener mKindredClickListener) {
		this.mKindredClickListener = mKindredClickListener;
	}

	public interface KindredClickListener {
		/**
		 * 左右点击监听
		 * @param position 左边为0，右边为1
		 */
		void addKindredClickListener(int position);
	}
}
